package cate.game.friend.po;

import cate.game.role.Role;
import cate.game.role.friend.FriendGiftItem;
import easy.java.dev.note.NoteField;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.data.annotation.Id;
import org.springframework.data.annotation.Transient;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class FriendBox {
    @NoteField(value = "邮箱数据更新日期")
    public Date updateDate;

    @NoteField(value = "未领取的邮件列表")
    private List<FriendGiftItem> list;

    @NoteField(value = "角色唯一id")
    @Id
    public String uid;

    @Transient
    private static final long expiredTime = 3600 * 24 * 1000 * 7L;

    public void initialize() {
        if (list == null) {
            list = new ArrayList<>();
        }
        list.removeIf(e -> e.recv
                || (System.currentTimeMillis() - e.sendTime) > expiredTime);
        if (updateDate == null) {
            updateDate = new Date();
        }
    }

    public void add(FriendGiftItem item) {
        this.list.add(item);
        this.updateDate = new Date();
    }

    public void addToRole(Role role) {
        try{
            for (FriendGiftItem item : this.list) {
                role.getFriend().recv(item, false);
            }
        }catch(Exception e){
            //失败也无所谓
        }
        this.list.clear();
    }

    public boolean isEmpty(){
        return CollectionUtils.isEmpty(list);
    }
}
